//
//  StatusViewModel.swift
//  SwiftWB
//
//  Created by 钟清 on 17/4/3.
//  Copyright © 2017年 Zhong Qing. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    
    var status: StatusesModel
    init(status: StatusesModel) {
        self.status = status
        /* 设置认证头像 */
        if let type = status.user?.verified_type {
            switch type {
            case 0:
                verified_image = UIImage(named: "avatar_vip")
            case 2,3,5:
                verified_image = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verified_image = UIImage(named: "avatar_grassroot")
            default:
                verified_image = nil
            }
        }
        /* 设置会员图标 */
        if status.user?.mbrank >= 1 && status.user?.mbrank <= 6 {
            vipImage = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        /* 获取格式化的创建时间 */
        if let timeStr = status.created_at {
            let createDate = NSDate.createDate(timeStr, formatterStr: "EE MM dd HH:mm:ss Z yyyy")
            create_Time = createDate.descriptionStr()
        }
        /* 获取格式化的来源 */
        if let sourceStr: NSString = status.source where sourceStr != "" {
            let startIndex = sourceStr.rangeOfString(">").location + 1
            let length = sourceStr.rangeOfString("<", options: NSStringCompareOptions.BackwardsSearch).location - startIndex
            let result = sourceStr.substringWithRange(NSMakeRange(startIndex, length))
            source_Text = "来自：" + result
        }
        /* 获取头像地址 */
        iconImage_URL = NSURL(string: status.user?.profile_image_url ?? "")
        /* 获取配图的URL */
        if let pic_dicts = (status.retweeted_status != nil) ? status.retweeted_status?.pic_urls : status.pic_urls {
            thumbnail_pic = [NSURL]()
            bmiddle_pic = [NSURL]()
            for dict in pic_dicts {
                guard var urlStr = dict["thumbnail_pic"] as? String else {
                    continue
                }
                let url = NSURL(string: urlStr)!
                thumbnail_pic?.append(url)
                /* 中图的URL */
                urlStr = urlStr.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                bmiddle_pic?.append(NSURL(string: urlStr)!)
            }
        }
        /* 获取转发微博的正文 */
        if let tempStr = status.retweeted_status?.text {
            let forwardName = status.retweeted_status?.user?.screen_name ?? ""
            forwardText = "@" + forwardName + ": " + tempStr
        }
    }
    
    /* 认证头像 */
    var verified_image: UIImage?
    /* 会员图标 */
    var vipImage: UIImage?
    /* 格式化的创建时间 */
    var create_Time: String = ""
    /* 格式化的来源 */
    var source_Text: String = ""
    /* 头像地址 */
    var iconImage_URL: NSURL?
    /* 获取配图的URL */
    var thumbnail_pic: [NSURL]?
    /* 中图的URL */
    var bmiddle_pic: [NSURL]?
    /* 转发微博的正文 */
    var forwardText: String?
    
}



